package com.embhealth.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.embhealth.dao.PermissionDao;
import com.embhealth.entity.PageResult;
import com.embhealth.entity.QueryPageBean;
import com.embhealth.pojo.Menu;
import com.embhealth.pojo.Permission;
import com.embhealth.service.PermissionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询基于mybatis框架的PageHelper分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<Permission> page = permissionDao.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 通过角色id查询所有相关权限
     * @param id
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findAllPmsByRole(Integer id,QueryPageBean queryPageBean){
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询基于mybatis框架的PageHelper分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<Permission> page = permissionDao.findAllPmsByRole(id);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public Permission handleUpdate(Integer id) {
        return permissionDao.handleUpdate(id);
    }

    /**
     * 添加权限
     *
     * @param permission
     */
    @Override
    public void handleAdd(Permission permission) {
        permissionDao.handleAdd(permission);
    }

    /**
     * 编辑更新权限
     *
     * @param permission
     */
    @Override
    public void handleEdit(Permission permission) {
        permissionDao.handleEdit(permission);
    }

    /**
     * 根据id删除权限
     *
     * @param id
     */
    @Override
    public boolean handleDelete(Integer id) {
        //通过权限id查询是否有与其他表的关联关系
        Integer count = permissionDao.findRoleByPermissionId(id);
        if (count > 0) {
            return false;
        }
        permissionDao.handleDelete(id);
        return true;
    }
}
